package cloud.pangui.core.log;

import cloud.pangui.core.constants.Env;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 设置预定义的logger配置，微服务只需引入core包，配置环境即可使用统一的配置项，无需再自己项目中再引入log4j.xml
 * 在ConfigFileApplicationListener之后
 * 在org.springframework.boot.context.logging.LoggingApplicationListener之前
 *
 * @Auther hepangui
 * @Date 2020/10/06 0001
*/

@Order(Ordered.HIGHEST_PRECEDENCE + 19)
public class CustomLoggerApplicationListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {

	/**
	 * 指定log4j2.xml的配置文件路径
	 */
	public static final String LOGGING_CONFIG = "logging.config";
	/**
	 * 指定输出文件的路径配置
	 */
	public static final String LOGGING_FILE_PATH = "logging.file.path";
	/**
	 * 基本配置的路径前缀
	 */
	public static final String LOGGING_CONFIG_PREFIX = "classpath:logconfig/log4j2-";

	/**
	 * 基本配置的路径后缀
	 */
	public static final String LOGGING_CONFIG_POSTFIX = ".xml";

	/**
	 * 输出日志文件的名称
	 */
	public static final String LOGGING_FILE_NAME = "logging.file.name";
	/**
	 * 应用名称
	 */
	public static final String SPRING_APPLICATION_NAME = "spring.application.name";


	/**
	 * log4j配置文件中的占位符，对应上述logging.file.path配置内容
	 */
	public static final String OCCUPY_LOG_PATH = "LOG_PATH";

	/**
	 * log4j配置文件中的占位符，对应上述logging.file.name配置内容
	 */
	public static final String OCCUPY_LOG_FILE = "LOG_FILE";


	/**
	 * logger
	 * @param applicationEvent
	 */
	@Override
	public void onApplicationEvent(ApplicationEnvironmentPreparedEvent applicationEvent) {
		List<String> profileList = Env.envs();

		ConfigurableEnvironment environment = applicationEvent.getEnvironment();
		String property = environment.getProperty(LOGGING_CONFIG);
		if (StringUtils.isEmpty(property)) {
			// spring.profiles.active
			String[] activeProfiles = environment.getActiveProfiles();
			for (String activeProfile : activeProfiles) {
				if (profileList.contains(activeProfile)) {
					if (!Env.LOCAL.getValue().equals(activeProfile)) {
						System.out.println("===================================");
						System.out.println("====== 非本地环境仅输出文件日志  ====");
						System.out.println("===================================");
					}
					System.setProperty(LOGGING_CONFIG, LOGGING_CONFIG_PREFIX + activeProfile + LOGGING_CONFIG_POSTFIX);
					break;
				}
			}
			if (StringUtils.isEmpty(environment.getProperty(LOGGING_CONFIG))) {
				System.setProperty(LOGGING_CONFIG, LOGGING_CONFIG_PREFIX + Env.LOCAL.getValue() + LOGGING_CONFIG_POSTFIX);
			}
		}
		// 日志输出文件夹
		String property1 = environment.getProperty(LOGGING_FILE_PATH);
		if (!StringUtils.isEmpty(property1)) {
			System.setProperty(OCCUPY_LOG_PATH, property1);
		}

		// 日志输出文件名
		property1 = environment.getProperty(LOGGING_FILE_NAME);
		if (!StringUtils.isEmpty(property1)) {
			System.setProperty(OCCUPY_LOG_FILE, property1);
		} else {
			// 默认为spring application name的值
			String property2 = environment.getProperty(SPRING_APPLICATION_NAME);
			if (!StringUtils.isEmpty(property2)) {
				System.setProperty(OCCUPY_LOG_FILE, property2);
			}
		}
	}
}

